#!/usr/bin/env sh

# to be used as a replacement for bash' $RANDOM

GSH_ROOT=$(cd "$(dirname "$0")/.." && pwd -P)
GSH_CONFIG=$GSH_ROOT/.config

n=${1:-1}

awk -v seed_file="$GSH_CONFIG/PRNG_seed" -v n="$n" -v PID=$$ 'BEGIN {
  getline seed < seed_file;
  if (seed) srand(seed); else srand();
  for (i=0; i<n; i++) {
    print int(rand()*65536);
  }
  if (seed) printf("%d\n", int(2^30 * rand())+PID) > seed_file;
}'
